package com.yodawnla.goldTree; 

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.MoveXModifier;
import org.anddev.andengine.entity.modifier.MoveYModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseBounceOut;

import com.yodawnla.lib.YoActivity;

public class Gold extends Sprite
{
	float mInitSize = 1f;
	boolean mIsCanTake = false;
	int mType = Values.Type.GOLD;
	
	public Gold(float pX, float pY, TextureRegion pTextureRegion)
	{
		super(pX, pY, pTextureRegion);
		setScale(0);
	}
	
	public void setInitSize(float size)
	{
		mInitSize = size;
	}
	
	public float getInitSize()
	{
		return mInitSize;
	}
	
	public boolean getIsCanTake()
	{
		return mIsCanTake;
	}
	
	public void setType(int type)
	{
		mType = type;
	}
	
	public int getType()
	{
		return mType;
	}
	
	public void startGrowUp()
	{
		IEntityModifierListener listener = new IEntityModifierListener()
		{
			@Override
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
			{
			}

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
			{
				mIsCanTake = true;
			}
		};
		
		setScaleCenterY(-20);
		float time = MathUtils.random(1.5f, 2.3f);
		float rotation = MathUtils.random(10, 30);
		clearEntityModifiers();
		registerEntityModifier(new ScaleModifier(1, 0, mInitSize, listener));	
		registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(
			new RotationModifier(time, rotation, -rotation),
			new RotationModifier(time, -rotation, rotation)
		)));
	}
	
	public void fall()
	{
		IEntityModifierListener mCoinListener = new IEntityModifierListener()
		{
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
			{
			}

			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
			{
				if(mType == Values.Type.BUG)
					YoActivity.getBaseActivity().playSound("SeBug");
				else
					YoActivity.getBaseActivity().playSound("SeCoin");
			}
		};
		
		IEntityModifierListener mRemoveListener = new IEntityModifierListener()
		{
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
			{
			}

			public void onModifierFinished(IModifier<IEntity> pModifier, final IEntity pItem)
			{
				YoActivity.getBaseActivity().runOnUpdateThread(new Runnable() 
				{
					public void run() 
					{
						pItem.detachSelf();
					}
				});
			}
		};
		
		int ground = MathUtils.random(400, 500);
		clearEntityModifiers();
		registerEntityModifier(new MoveYModifier(1f, getY(), ground, EaseBounceOut.getInstance()));
		registerEntityModifier(new SequenceEntityModifier(mRemoveListener,
			new AlphaModifier(0.3f, 1, 1, mCoinListener),
			new MoveXModifier(0.2f, getX(), getX() + MathUtils.random(-35, 35)),
			new AlphaModifier(0.5f, 1, 1),
			new AlphaModifier(0.5f, 1, 0)
		));
	}
}